<?php include_once "top.php";
$user=$db->ig2_want('mae_user','id='.$_GET['id'].' and is_delete=0');
if(!$user) tellgoto('','user.php');
$sells=count($db->ig2_select('mae_sell','uid='.$user['id'].' and create_time >= 1447070400'));
$answer_ids=explode(',',$user['answer_ids']);
$count_answer_ids=trim($user['answer_ids'])?count($answer_ids):0;

// 只有在分组控制内的门店员工才能参加答题活动
$surplus = 0;
$now = time();
$activeSql = "SELECT aa.* FROM mae_answer_activity aa";
$activeSql .= " INNER JOIN mae_rel_answer_activity_group raag ON raag.activityid = aa.id";
$activeSql .= " INNER JOIN mae_group g ON g.id = raag.groupid";
$activeSql .= " INNER JOIN mae_rel_group_shop rgs ON rgs.groupid = g.id";
$activeSql .= " INNER JOIN mae_shop s ON s.id = rgs.shopid";
$activeSql .= " INNER JOIN mae_role r ON r.shop_id = s.id";
$activeSql .= " INNER JOIN mae_user u ON u.id = r.uid";
$activeSql .= " WHERE aa.status = 1 AND aa.starttime <= ".$now." AND aa.endtime >= ".$now." AND u.id = ".$user['id'];
$activity = $db->ig2_query_select($activeSql);
if ($activity) {
    $per = 3;
    if ($sells >= $per) {
        $surplus = floor($sells/$per)-$count_answer_ids;
    }
    $surplus=$surplus>0?$surplus:0;
}


$where='1';
$where.=' order by rand() limit 0,'.$surplus;
?>
<link href="css/discount.css" rel="stylesheet" type="text/css" />
<link href="css/common.css" rel="stylesheet" type="text/css" />
<div class="container">
	<div class="p6">
		<img class="p6bg bg" src="images/p6bg.jpg">
		<span class="p2text p6text" style="color:#fff;font-size:12pt;">您还可答<label id="number"><?php echo $surplus?></label>道题</span>
		<div id="p6bar">
			<?php foreach($db->ig2_select('mae_question',$where) as $list){?>
			<div id="li_<?php echo $list['id']?>"><li>
				<label class="p6text" style="font-size:14pt;">问题<?php echo $list['id']?>：<?php echo $list['content']?></label>
				<?php foreach($db->ig2_select('mae_answer','question_id='.$list['id'].' order by id asc') as $li){?>
				<label  style="font-size:14pt;" class="p6texts" id="answer_<?php echo $li['id']?>" onclick="answer('<?php echo $list['id']?>','<?php echo $li['id']?>')"><img class="p6bars" src="images/option_1.png"><?php echo $li['content']?></label>
				<?php }?>
				<input id="question_answer_<?php echo $list['id']?>" type="hidden" name="" value="0" />
				<img class="p6bt" id="p6bt_<?php echo $list['id']?>" src="images/p6bt.jpg">
                <script>
					document.getElementById("p6bt_<?php echo $list['id']?>").addEventListener("touchstart", touchStartp6bt_<?php echo $list['id']?>, false);
					document.getElementById("p6bt_<?php echo $list['id']?>").addEventListener("touchend", touchEndp6bt_<?php echo $list['id']?>, false);
					function touchStartp6bt_<?php echo $list['id']?>(event){
						startX = event.touches[0].clientX;
						document.getElementById('p6bt_<?php echo $list['id']?>').src='images/p6bt_h.jpg';	
					}
					function touchEndp6bt_<?php echo $list['id']?>(event){
						endX = event.changedTouches[0].clientX;
						document.getElementById('p6bt_<?php echo $list['id']?>').src='images/p6bt.jpg';
					}	
				</script>				
			</li></div>
			<?php }?>
		</div>
		<img id="correct" class="bg" src="images/correct.png">
		<img id="correct2" src="images/p2bar_3.png">
		<img id="correct3" class="bg" src="images/correct3.png">
	</div>
</div>
<script type="text/javascript">
$('#correct2').click(function(){
	$('#correct,#correct2,#correct3').fadeOut(500);
});

//选择答案操作
function answer(qid,aid){
	var answer=document.getElementById('question_answer_'+qid).value;
	if(answer != aid){
		document.getElementById('answer_'+aid).firstChild.src='images/option_2.png';
		if(answer != 0){
			document.getElementById('answer_'+answer).firstChild.src='images/option_1.png';
		}
		document.getElementById('question_answer_'+qid).value=aid;
	}
}

//提交问题答案操作
$('.p6bt').click(function(){
	//$(this).unbind('click');
	var qid=$(this).attr('id').substr(5);
    var aid=document.getElementById('question_answer_'+qid).value;
	var uid='<?php echo $user['id']?>';

	if(aid == 0){
		alert('您还没有选择答案');
	}else{
        $.ajax({
            type: "GET",
            url: "ra_ajax_answer.php",
            dataType: 'json',
            data: {
                method: "checkanswer",
                qid: qid,
                aid: aid,
                uid: uid
            },
            success: function(data){
                if(data.errcode == 0 || data.errcode == 1){
					$(".answerpop_body_txt1").html(data.title);
                    $(".answerpop_body_txt2").html(data.detail);
                    $(".answerpop_body_txt3").html(data.nomore);
					var number=parseInt($('#number').html());
					$('#number').html(number-1);
					$('#li_'+qid).html('');

                    var windowWidth = $(window).outerWidth();
                    var windowHeight = 578;
                    var popupHeight = $(".answerpop_body").height();
                    var popupWidth = $(".container").width();
                    $(".answerpop_body").css({"position": "absolute","width":popupWidth+"px"}).animate({left: windowWidth/2-popupWidth/2, top: windowHeight/2-popupHeight/2, opacity: "show" }, "slow");
                    $(".pop_mask").fadeIn(500);
				} else {
                    $(".answerpop_body_txt1").html(data.title);
                }
            }
        });
	}
});

function getWidth(){
	$('.p1,.p2,.p3,.p4,.container,.bg').width(x);
	$('.p1,.p2,.p3,.p4,.p5,.p6,.p7,.p8').css({left:0,top:0});
	//p6
	$('.p6text').css({left:501/p+'px',top:110/p+'px'});
	$('#p6bar').width(516/p).height(610/p).css({left:(x-516/p)/2+'px',top:350/p+'px'});
	$('#p6bar li').width(516/p).css({marginTop:30/p+'px'});
	$('.p6bars').width(60/p).height(60/p).css({marginRight:20/p+'px',marginBottom:10/p+'px'});
	$('.p6bt').width(112/p).height(55/p).css({marginLeft:90/p+'px',marginTop:30/p+'px'});
	$('#p6bar li label').css({width:516/p,left:100/p+'px',top:55/p+'px',marginTop:15/p+'px',lineHeight:60/p+'px'});
	$('.p6text').css({marginLeft:0});
	$('#correct,#correct3').css({left:0,top:0});
	$('#correct2').width(30/p).height(32/p).css({left:641/p+'px',top:440/p+'px'});
}

$(document).ready(function(){
    $("#closeModal").click(function(){
        $(".answerpop_body").fadeOut(500);
        $(".pop_mask").fadeOut(500);
    });
});
</script>
<div class='pop_mask'style="display:none; width:100%;height:100%"></div>
<div class="answerpop_body" style="display:none;">
    <div class="answerpop_body_inner">
        <div class="answerpop_body_cancel"><a href="javascript:void(0);" id="closeModal"><img src="images/pop1_cancel.png" width="40" height="40" /></a></div>
        <div class="answerpop_body_txt">
            <div class="answerpop_body_txt1"></div>
            <div class="answerpop_body_txt2"></div>
            <div class="answerpop_body_txt3"></div>
        </div>

    </div>
</div>
</body>
</html>